package com.boc.address.activity;

import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.boc.address.R;
import com.boc.address.R2;
import com.boc.address.adapter.AddressListAda;
import com.boc.address.bean.AddressListBean;
import com.boc.address.contract.AddressListContract;
import com.boc.address.presenter.AddressListPresenter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import demo.bocweb.com.sdk.base.BasePresenter;
import demo.bocweb.com.sdk.base.activity.BaseMVPActivity;
import demo.bocweb.com.sdk.baseApi.RequestHelper;
import demo.bocweb.com.sdk.config.ARoutConfig;
import demo.bocweb.com.sdk.manager.MessageEvent;
import demo.bocweb.com.sdk.util.Loading;
import demo.bocweb.com.sdk.util.ToolbarHelper;
import okhttp3.RequestBody;

/**
 * 收货地址列表
 * Created by zhelun on 2018/2/1.
 */
@Route(path = ARoutConfig.addressListPath)
public class AddressListActivity extends BaseMVPActivity<AddressListContract.IAddressListPresenter, AddressListContract.IAddressListModel>
        implements AddressListContract.IAddressListView {
    @BindView(R2.id.recycler)
    XRecyclerView recycler;

    private AddressListAda addressListAda;
    private List<AddressListBean> addresses = new ArrayList<>();
    private AlertDialog dialog;

    @NonNull
    @Override
    public BasePresenter initPresenter() {
        return new AddressListPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_addresslist;
    }

    @Override
    protected void initData() {
        super.initData();
        ToolbarHelper.init(this, R.string.str_title_address_list, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        }, "添加", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ARouter.getInstance().build(ARoutConfig.addAddressPath).navigation();
            }
        });
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setLoadingMoreEnabled(false);
        addressListAda = new AddressListAda(false, this);
        recycler.setAdapter(addressListAda);
        Loading.show(this, "加载中...");
        getAddressList();
        recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                getAddressList();
            }

            @Override
            public void onLoadMore() {

            }
        });

    }

    private void getAddressList() {
        mPresenter.getAddressList();
    }

    @Override
    public void updateAddressList(List<AddressListBean> beans) {
        recycler.refreshComplete();
        addresses.clear();
        addresses.addAll(beans);
        addressListAda.resetItems(addresses);

    }

    @Override
    public void setDefaultSuccess() {
        getAddressList();
    }

    @Override
    public void deleteSuccess() {
        getAddressList();
    }

    @Override
    public void delete(final int position) {
        if (dialog == null) {
            dialog = new AlertDialog.Builder(this)
                    .setTitle("删除收货地址")
                    .setMessage("是否删除该条收货地址？")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    })
                    .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Loading.show(AddressListActivity.this, "删除中...");
                            RequestBody body = RequestHelper.build().add("Id", addresses.get(position).getAddressId()).create();
                            mPresenter.deleteAddress(body);
                        }
                    }).show();
        } else {
            dialog.show();
        }


    }

    @Override
    public void setDefault(int position) {
        Loading.show(this, "设置中...");
        RequestBody body = RequestHelper.build().add("AddressId", addresses.get(position).getAddressId()).create();
        mPresenter.setDefaultAddress(body);
    }

    @Override
    public void onMainEvent(MessageEvent messageEvent) {
        super.onMainEvent(messageEvent);
        if (messageEvent.getMessage().equals("addAddress")) {
            getAddressList();
        }
    }
}
